<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
Object obj=request.getSession().getAttribute("role_resource");
if(null==obj){
	response.setStatus(1000);
	return;
}
%> 
   <script type="text/javascript">
   		var nodecheck=false;
		var store = new Ext.data.JsonStore({
			root: "data",//从返回的json对象data中读取数据
			fields: ["rid", "rname","did", "dname", "oid", "oname","remark"],
  			url:"role!listByDepID.action"
    	});
    	//获取部门信息
		var store2 = new Ext.data.JsonStore({
			root: "data",//从返回的json对象data中读取数据
			fields: ["id","name"],
  			url:"depart!listByOrg.action"
    	});
    	//获取机构信息
		var store3 = new Ext.data.JsonStore({
			root: "data",//从返回的json对象data中读取数据
			fields: ["rid", "rname","did", "dname", "oid", "oname","remark"],
  			url:"role!listByDepID.action"
    	});    	
    	var reqtype="";
    	function request(rid){
    		var ajaxurl="";
	    	var par={};
    		if("del"==reqtype){
    			ajaxurl="role!del.action";
    			par={
    				"role.rid":rid
    			};
    		}else{
	    		var form=Ext.getCmp("p2form");
	    		//获取原始数据
	    		var formData=form.showData;
	    		//获取选择的功能菜单
	    		var t_funs=Ext.getCmp("rolefuncpanel").getChecked();
				var funs="";
				for(var i=0;i<t_funs.length;i++){
					funs+="|"+t_funs[i].attributes.id;
				}
				funs=funs.substring(1);
				var node=Ext.getCmp("roleTree").getSelectionModel().getSelectedNode();
	    		//如果为添加操作
	    		if("add"==reqtype){
	    			ajaxurl="role!add.action";
	  				par={
	  					"role.rname":form.getComponent("rname").getValue(),
						"role.did":node.id,
						"role.rolefun":funs,
						"role.remark":form.getComponent("remark").getValue()
					};
	    		}//如果为修改操作
	    		else if("edit"==reqtype){
	    			ajaxurl="role!updata.action";
	    			par={
						"role.rid":form.getComponent("rid").getValue(),
						"role.rname":form.getComponent("rname").getValue(),
						"role.did":node.id,
						"role.olddid":formData.did,
						"role.rolefun":funs,
						"role.remark":form.getComponent("remark").getValue()
					};
	    		}
    		}
			Ext.Ajax.request({
				clientValidation: true,
				params:par,
				url:ajaxurl,
				success: function(response){
					var d=eval("("+response.responseText+")");
					if(d.code == "0"){
						Ext.Msg.show({
						   title:"提示",
						   msg:"操作成功！",
						   buttons:Ext.Msg.OK,
						   icon: Ext.MessageBox.INFO
						});
						
						store.load();//获取部门信息
						Ext.getCmp("role_panel").layout.setActiveItem(0);
					} else {
						Ext.Msg.show({
						   title:"提示",
						   msg:"操作失败！",
						   buttons:Ext.Msg.OK,
						   icon: Ext.MessageBox.ERROR
						});
					}
					
					Ext.getCmp("save").setDisabled(false);
				}
			});
    	}
    	
    	//添加，修改角色前验证角色名是否已存在
    	//如存在则提示，不存在则允许操作
    	function checkRoleName(){
    		var ajaxurl="role!checkRoleName.action";
	    	var par={};
    		var form=Ext.getCmp("p2form");

			var node=Ext.getCmp("roleTree").getSelectionModel().getSelectedNode();
    		//如果为添加操作
    		if("add"==reqtype){
  				par={
  					"role.rname":form.getComponent("rname").getValue(),
					"role.did":node.id
				};
    		}//如果为修改操作
    		else if("edit"==reqtype){
    			par={
					"role.rid":form.getComponent("rid").getValue(),
					"role.rname":form.getComponent("rname").getValue(),
					"role.did":node.id
				};
    		}
			Ext.Ajax.request({
				clientValidation: true,
				params:par,
				url:ajaxurl,
				success: function(response){
					var d=eval("("+response.responseText+")");
					if(d.code != "0"){
						form.getComponent("rname").setValue("");
						Ext.Msg.show({
   							title:"提示",
   							msg:"该角色名已存在，请重新输入！",
   							buttons:Ext.Msg.OK,
   							icon: Ext.MessageBox.ERROR
						});
					}
				}
			});
    	}
    	
		var p1 = {
			id: "rolegrid",
			xtype: "grid",
			stripeRows:true,
			loadMask : true,//在加载数据时遮罩住元素,替换元素Updater的加载提示器，并且在初始化加载之后销毁
			autoExpandColumn:"remark",//自动填充
			autoExpandMin : 100,//自动填充最小宽度
			store:store,
			headersDisabled: true,
			columns:[
	        	new Ext.grid.RowNumberer({header: "序号", width: 35}),
	        	{header: "角色编号", dataIndex: "rid", menuDisabled: true},
	        	{header: "角色名称", dataIndex: "rname", menuDisabled: true},
	        	{header: "部门名称", dataIndex: "dname", menuDisabled: true},
				{header: "机构名称", dataIndex: "oname", menuDisabled: true},
				{header: "备注",dataIndex:"remark",id:"remark", menuDisabled: true}
			],
			bbar: new Ext.ux.PagingToolbar({
				pageSize: limit,
				store:store
			}),
        	buttonAlign:"center",
			buttons:[{text:"新增", handler: function(){
				reqtype="add";
				//获取部门树中的选择项
				var node=Ext.getCmp("roleTree").getSelectionModel().getSelectedNode();
				//如果有选择节点并且是叶子节点
				if(node && node.isLeaf()){
					var pnode=node.parentNode;//获取父节点
					Ext.getCmp("p2form").resetParam(node.text,pnode.text);
					Ext.getCmp("role_panel").layout.setActiveItem(1);//激活显示第二页
					Ext.getCmp("rolefuncpanel").getRootNode().reload();
				}else{
					Ext.Msg.show({
					   title:"警告",
					   msg: "请选择要添加角色的部门",
					   buttons: Ext.Msg.OK,
					   animEl: "elId",
					   icon:Ext.MessageBox.WARNING
					});
				}
			}},{text:"修改", handler: function(){
				reqtype="edit";
				var record=Ext.getCmp("rolegrid").getSelectionModel().getSelected();//获取列表中被选择的数据
				if (record){
					Ext.getCmp("p2form").setParam(record.data);
					var node=Ext.getCmp("rolefuncpanel").getRootNode();
					node.setId(record.data.rid);
					node.reload();
					Ext.getCmp("role_panel").layout.setActiveItem(1);//激活显示第二页
				}else{
					Ext.Msg.show({
					   title:"警告",
					   msg: "请选择要修改的角色",
					   buttons: Ext.Msg.OK,
					   animEl: "elId",
					   icon: Ext.MessageBox.WARNING
					});
				}
			}},{text:"删除",handler: function(){
				var p1 = this.ownerCt.ownerCt;
				var record = Ext.getCmp("rolegrid").getSelectionModel().getSelected();
				if (record){
					reqtype="del";
					Ext.Msg.show({
					   title:"提示",
					   msg:"是否确认删除该数据?",
					   buttons: Ext.Msg.OKCANCEL,
					   fn:function(btn, text){
						    if (btn == 'ok'){
								Ext.Ajax.request({
									clientValidation: true,
									params: {
										"role.rid": record.data.rid
									},
									url: "role!checkDelRole.action",
									success: function(response){
										var d=eval("("+response.responseText+")");
										if(d.code == "0"){
											request(record.data.rid);
										} else {
											Ext.Msg.show({
					   							title:"提示",
					   							msg:"该角色已绑定用户，禁止删除！",
					   							buttons:Ext.Msg.OK,
					   							icon: Ext.MessageBox.ERROR
											});
										}
									}
								});
						    }
					   },
					   animEl:'elId',
					   icon: Ext.MessageBox.WARNING
					});
				}else{
					Ext.Msg.show({
					   title:"警告",
					   msg: "请选择要删除的角色",
					   buttons: Ext.Msg.OK,
					   animEl: "elId",
					   icon: Ext.MessageBox.WARNING
					});
				}
			}}]
		};
		//--------------------------------------------------------------------
		var p2 = {
			id:"p2form",
			title:"角色信息",
		    labelWidth: 60,
			xtype: "form",	
			border : false,
			region: "north",
			height:180,		
		    bodyStyle:"padding:5px 5px 0",
			defaultType: "textfield",
			items: [{
				itemId: "rid",
				disabled:true,
				fieldLabel: "角色编号",
				anchor:"100%",
				name: "rid"
			},{
				itemId: "rname",
				fieldLabel: "角色名称",
				anchor:"100%",
				name: "rname",
				vtype:"alphanumchinese",
				validator:function (val){
	        		if(val=="")return "该输入项为必输项！";
	        		if(Ext.fucCheckCHLength(val)>50){
	        			return "请输入50个字符范围内的值！<br>注：一个中文字符算两个字符的长度！";
	        		}
	        		return true;
	        	},
	        	listeners: {
					'blur': function(){
						checkRoleName();
					}
				}
			},{
				itemId: "dname",
				fieldLabel: "部门名称",
				anchor:"100%",disabled:true,
				name:"dname"
			}, {
				itemId: "oname",
				disabled:true,
				fieldLabel: "机构名称",
				anchor:"100%",
				name: "oname"
			}, {
				itemId: "remark",
				fieldLabel: "备注",
				anchor:"100%",
				name: "remark",
				vtype:"alphanumchinese",
				validator:function (val){
	        		if(val=="")return true;
	        		if(Ext.fucCheckCHLength(val)>200){
	        			return "请输入200个字符范围内的值！<br>注：一个中文字符算两个字符的长度！";
	        		}
	        		return true;
	        	}
			}],
			showData:null,
			setParam: function(data){
				var me = this;
				me.showData=data;
				me.getComponent("rid").setValue(data.rid);//角色ID
				me.getComponent("rid").show();
				me.getComponent("rname").setValue(data.rname);//角色名称
				me.getComponent("dname").setValue(data.dname);//部门名称
				me.getComponent("oname").setValue(data.oname);//机构名称
				me.getComponent("remark").setValue(data.remark);//备注
				},
			resetParam: function(dname,oname){
				var me = this;
				me.getComponent("rid").setValue("");//角色ID
				me.getComponent("rid").hide();//角色ID
				me.getComponent("rname").setValue("");//角色名称
				me.getComponent("dname").setValue(dname);//部门名称
				me.getComponent("oname").setValue(oname);//机构名称
				me.getComponent("remark").setValue("");//备注
			}
		};
		//--------------------------------------------------------------------
		var p3=new Ext.Panel({
			id:"rule_p3",
			layout: "border",
			items: [p2,{
			    xtype:"treepanel"
			    ,region: "center"
				,checkModel: 'cascade'   //对树的级联多选   
				,onlyLeafCheckable: false//对树所有结点都可选   
			    ,id:"rolefuncpanel"
				,width : "100%"
				,split: false //分割线可拖动
				,animate : true//展开折叠有动画效果
				,border : false //不显示边框
				,lines : true//不显示节点连线
				,autoScroll:true//滚动条
				,title:"角色权限设置"
				,rootVisible:false
				,root: new Ext.tree.AsyncTreeNode({ id:'root' })
				,loader: new Ext.tree.TreeLoader({
					url:"treeCheck!rolefunc.action"
			        ,baseAttrs:{uiProvider:Ext.ux.TreeCheckNodeUI} //添加 uiProvider 属性   
			    })
			}],
			buttonAlign:"center",
			buttons:[{
					text:"保存",id:"save", handler: function(e){
					if(!Ext.getCmp("p2form").getForm().isValid()){
						Ext.Msg.alert("提示","验证不通过！");
						return;
					}
					
					e.setDisabled(true);
					Ext.getCmp("role_panel").layout.setActiveItem(0);
					Ext.getCmp("rolefuncpanel").expandAll();
					request();}
				},{
					text:"取消", handler: function(){
						Ext.getCmp("role_panel").layout.setActiveItem(0);//显示第二界面
					}
				}
			]
			
		});
		
		var role_pel ={
			id:"xxxpanel",
			xtype:"panel",
			border : false,
			layout: "border",
			items: [{
					id:"roleTree",
					xtype: "billTreePanel",
					dataUrl: "tree!role.action",
					root: new Ext.tree.AsyncTreeNode({
						id:"root",//跟节点ID
						expanded : true//是否展开
					}),
					listeners: {
						"click": function(node){
							store.baseParams={};
							if(node.isLeaf()){
								store.baseParams["role.did"]=node.id;
								store.baseParams["start"]=0;
								store.baseParams["limit"]=limit;
								store.load();//获取部门信息
								Ext.getCmp("role_panel").layout.setActiveItem(0);//显示第二界面
							}
						}
					}
				},{
					id: "role_panel",
					region: "center",
					layout:"card",
					border : false, 
					activeItem:0,//显示第一个容器
					items: [p1, p3]
			}]
		};
		
	var CenterPanel=Ext.getCmp("CenterPanel");
	CenterPanel.removeAll();
	CenterPanel.add(role_pel);
	CenterPanel.doLayout();
	</script>
